class Admin::MessagesController < Admin::ApplicationController

  def index
    @project_messages = ProjectMessage.where(reply_id: [0,nil]).page( params[:page] )
  end

  def destroy
    @project_messages = ProjectMessage.find(params[:id])
    if !@project_messages.blank?
      @project_messages.update_attributes(:deleted_at => Time.now.utc)
      render_success("[项目删除成功]")
    else
      render_fail("[项目未找到]")
    end

    return
  end

  def show
    @project_messages = ProjectMessage.find(params[:id])
    @reply_messages = ProjectMessage.where('reply_id' => params[:id]).first
    if @reply_messages.blank?
      @reply_message = ''
    else
      @reply_message = @reply_messages['message']
    end
  end

  def edit
    @project_messages = ProjectMessage.find(params[:id])
    @reply_messages = ProjectMessage.where('reply_id' => params[:id]).first
    if @reply_messages.blank?
      @reply_messages = ProjectMessage.new
      @reply_messages['message'] = params[:message]
      @reply_messages['project_id'] = @project_messages['project_id']
      @reply_messages['user_id'] = current_user.id
      @reply_messages['reply_id'] = @project_messages['id']
      @reply_messages['isshow'] = true
      @reply_messages.save
    else
      @reply_messages['message'] = params[:message]
      @reply_messages.save
    end
    redirect_to :action => 'index'
  end

  def verify
    @project_messages = ProjectMessage.find(params[:id])
    if !@project_messages.blank?
      @project_messages.update_attributes(:isshow => true)
      render_success("[项目审核成功]")
    else
      render_fail("[项目未找到]")
    end
    return
  end

  # 回复
  private
  def post_params
    params.permit(:isshow, :message, :reply_id)
  end
  # 审核
  private
  def verify_params
    params.permit(:isshow, :message)
  end


end
